<?php

class MprankFace extends FaceBase
{
	private $nohf = ['rankdailymore',  'diydel', 'diyeditwxid', 'rankweeklymore'];
	private $mysql = null;
	private $api = null;
	private $weixin = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header();
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->api = n_api('mprank');
		$this->weixin = n_api('weixin');
	}

	public function rankDaily()
	{
		$stime = microtime(true);
		$param = $this->param('
			cid:uint:ignore,
			date:char:ignore,
			page:page
		');
		$data['page'] = $param['page'];
		$data['cid'] = empty($param['cid'])?1:$param['cid'];
		$data['perpage'] = $perpage = 50;
		$categories = $this->weixin->getCategories();

		$category = $categories[$data['cid']];

		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $this->api->getMaxRankDay();
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$data['categories'] = $categories;
		$data['category'] = $category;
		$data['date'] = $date;

		$data['total'] = $this->weixin->get_total_count();

		$data['rank_mplist'] = $this->api->getMprankByCategoryId($category['id'], $date, 'day', $start, $perpage);
		$data['page'] = $page;

		if (isset($_SESSION['ppid'])){
			$data['cart'] = n_api('cart')->getCartByPpid($_SESSION['ppid']);
		}
		foreach($data['categories'] as $key=>$val){
			$data['cateTree'][$val['first_category']][] = $val;
		}

		$this->_html('rankDaily', $data);
	}

	public function rankDailyMore()
	{
		$param = $this->param('
			cid:char:ignore,
			date:char:ignore,
			page:char:ignore
		');

		$perpage = 20;

		if (!isset($param['cid'])) {
			echo json_encode(n_state(false,"暂无一下页"));
			return;
		}

		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = strtotime(date('Y-m-d', strtotime('-1 day')));
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$rs = $this->api->getMprankByCategoryId($param['cid'], $date, 'day', $start, $perpage);
		if (count($rs) == 0)
			echo json_encode(n_state(false,"暂无一下页"));
		else {
			$rank_mplist = [];
			foreach ($rs as $value) {
				$value['rank'] = ++$start;
				$rank_mplist[] = $value;
			}
			$rs = n_state(true,"成功");
			$rs['data'] = $rank_mplist;
			echo json_encode($rs);
		}
	}

	public function rankWeekly()
	{
		$param = $this->param('
			cid:uint:ignore,
			date:char:ignore,
			page:page
		');
		$data['page'] = $param['page'];
		$data['cid'] = empty($param['cid'])?1:$param['cid'];
		$data['perpage'] = $perpage = 50;
		$categories = $this->weixin->getCategories();

		$category = $categories[$data['cid']];

		$maxWeek = $this->api->getMaxRankDay();
		if (isset($param['date'])) {
			$dates = explode(' - ',$param['date']);
			$sdate = strtotime($dates[0]);
			$edate = strtotime($dates[1]);
		} else {
			$edate = $maxWeek;
			$sdate = $edate - 7*24*60*60;
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$data['categories'] = $categories;
		$data['category'] = $category;
		$data['sdate'] = date('m/d/Y',$sdate);
		$data['edate'] = date('m/d/Y',$edate);
		$data['mdate'] = date('m/d/Y',$maxWeek);
		
		$data['total'] = $this->weixin->get_total_count();
		
		$data['rank_mplist'] = $this->api->getMprankByCategoryIdBetween($category['id'], $sdate, $edate, $start, $perpage);
		$data['page'] = $page;
		if (isset($_SESSION['ppid']))
			$data['cart'] = n_api('cart')->getCartByPpid($_SESSION['ppid']);
		foreach($data['categories'] as $key=>$val){
			$data['cateTree'][$val['first_category']][] = $val;
		}
		$this->_html('rankWeekly', $data);
	}

	public function rankWeeklyMore()
	{
		$param = $this->param('
			cid:char:ignore,
			date:char:ignore,
			page:char:ignore
		');

		$perpage = 20;

		if (!isset($param['cid'])) {
			echo json_encode(n_state(false,"暂无一下页"));
			return;
		}

		$maxWeek = $this->api->getMaxRankWeek();
		$weekArr = [$maxWeek, $maxWeek-7*24*3600, $maxWeek-2*7*24*2600];
		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $maxWeek;
		}

		if (isset($param['page'])) {
			$page = $param['page'];
		} else {
			$page = 1;
		}

		$start = ($page -1) * $perpage;
		$rs = $this->api->getMprankByCategoryId($param['cid'], $date, 'week', $start, $perpage);
		if (count($rs) == 0)
			echo json_encode(n_state(false,"暂无一下页"));
		else {
			$rank_mplist = [];
			foreach ($rs as $value) {
				$value['rank'] = ++$start;
				$rank_mplist[] = $value;
			}
			$rs = n_state(true,"成功");
			$rs['data'] = $rank_mplist;
			echo json_encode($rs);
		}
	}

	public function rankdiy()
	{
		$ppid = $_SESSION['ppid'];
		$diyname = $this->api->getDiyByPpid($ppid);

		$this->_html('rankdiy', $diyname);
	}

	public function diyAdd()
	{
		$this->_html('diyAdd');
	}

	public function diyDoAdd()
	{
		$param = $this->param('diyname:char:empty');
		$diyname = $param['diyname'];

		$ppid = $_SESSION['ppid'];
		$time = time();
		$data = [
			'diyname' => $diyname,
			'ppid' => $ppid,
			'ctime' => $time,
			'utime' => $time
		];
		$this->mysql->insert('mprank_diy', $data);
		n_mod("skin")->save("保存成功",3);
	}

	public function diyEdit()
	{
		$param = $this->param('diyId:char:empty');
		$diyId = $param['diyId'];
		$ppid = $_SESSION['ppid'];

		$query = "select * from mprank_diy where id = $diyId and ppid = $ppid";
		$data['diyInfo'] = $this->mysql->fetchFirst($query);
		$carts = n_api('cart')->getCartByPpid($ppid);
		$cartDetails = n_api('weixin')->getWxDetail($carts);
		foreach ($cartDetails as $value) {
			$cartDetailRs[$value['wxid']] = $value;
		}
		$data['cartDetails'] = $cartDetailRs;
		$this->_html('diyEdit', $data);
	}

	public function diyEditWxid()
	{
		$param = $this->param('wxIds:char:empty,diyId:char:empty');

		$wxIds = json_decode(stripslashes($param['wxIds']), true);
		$diyId = $param['diyId'];
		if (count($wxIds) > 0) {
			$wxIdEncode = json_encode($wxIds);
			$ppid = $_SESSION['ppid'];
			$query = "update mprank_diy set wxid_set = '$wxIdEncode', utime = " . time() . " where id = $diyId and ppid = $ppid";
			$this->mysql->exec($query);
			echo "ok";
		}
	}

	public function show()
	{
		$param = $this->param('diyId:char:empty');
		$diyId = $param['diyId'];
		$ppid = $_SESSION['ppid'];

		$query = "select * from mprank_diy where id = $diyId and ppid = $ppid";
		$diyDetails = $this->mysql->fetchFirst($query);

		$wxidSet = json_decode($diyDetails['wxid_set'], true);
		$date = $this->api->getMaxRankDay();

		$rankMplist = $this->api->getMprankByWxids($wxidSet, $date);
		foreach ($rankMplist as $value) {
			$rankWxidSet[] = $value['wxid'];
		}
		$weixin = n_api('weixin');
		$rs = $weixin->getWxDetail($rankWxidSet);
		
		foreach ($rs as $value) {
			$wxInfos[$value['wxid']] = $value;
		}
		$data = [];
		foreach ($rankMplist as $value) {
			$value['wxname'] = $wxInfos[$value['wxid']]['wxname'];
			$data[] = $value;
		}

		$data['rank_mplist'] = $data;
		unset($diyDetails['wxid_set']);
		$data['diyInfo'] = $diyDetails;
		$this->_html('diyShow', $data);
	}

	public function diyDel()
	{
		$param = $this->param('diyId:char:empty');
		$diyId = $param['diyId'];
		$ppid = $_SESSION['ppid'];

		$query = "delete from mprank_diy where id = $diyId and ppid = $ppid";
		$this->mysql->exec($query);
		n_mod("skin")->save("删除成功",3);
	}
	public function search(){
		$param = $this->param("group:char,page:page");
		$data['group'] = $param['group'];
		$data['page'] = $param['page'];
		$data['perpage'] = 20;
		$data['start'] = $start = ($param['page']-1)*$data['perpage'];
		$data['total'] = 0;
		if(!empty($param['group'])){
			
			$group = $this->weixin->search($param['group']);
			if(!empty($group)){
				
				$wxids = [];
				foreach($group as $key=>$val){
					$wxids[] = $val['wxid'];
				}
				$date = $this->api->getMaxRankWeek();
				$data['rank_mplist'] = $this->api->getMprankWeeklyByWxids($wxids,$date,$start,$data['perpage']);
				$data['total'] = $this->api->getMprankByWxidsCount($wxids,$date);
			}
		
		}

		$this->_html("search",$data);
	}
	public function favDaily(){
		$param = $this->param('
			date:char:ignore
		');
		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $this->api->getMaxRankDay();
		}
		
		$fav = explode('----',$_COOKIE['fav']);
		if(!empty($fav)){
			$data['rank_mplist'] = $this->api->getMprankByWxids($fav,$date);
		}


		$data['date'] = $date;

		$this->_html('favDaily', $data);
	}
	public function favWeekly(){
		$param = $this->param('
			date:char:ignore
		');
		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = $this->api->getMaxRankWeek();
		}

		$fav = explode('----',$_COOKIE['fav']);
		if(!empty($fav)){
			$data['rank_mplist'] = $this->api->getMprankWeeklyByWxids($fav,$date);
		}

		$data['date'] = $date;

		$this->_html('favWeekly', $data);
	}
	
	public function __destruct(){
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->footer();
		}
	}
}